const Compiler = require('./Compiler')
const NodeEnvironmentPlugin = require('./node/NodeEnvironmentPlugin')
const WebpackOptionsApply = require('./WebpackOptionsApply')

const webpack = function (options) {
    // 1. 实例化compiler对象
    let compiler = new Compiler(options.context)
    compiler.options = options
    // 2. 初始化NodeEnvironmentPlugin(让compiler具备文件读写能力)
    new NodeEnvironmentPlugin().apply(compiler)
    // 3. 挂载所有的plugins插件至compiler对象身上
    if (options.plugins && Array.isArray(options.plugins)) {
        for (const plugin of options.plugins) {
            plugin.apply(compiler)
        }
    }
    // 4. 挂载所有webpack内置的插件(入口)
    new WebpackOptionsApply().process(options, compiler)
    // 5. 返回compiler对象
    return compiler
}

module.exports = webpack